package drds.connection_pool.proxy;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public abstract class ResultSetProxy implements ResultSet
{
   protected final ConnectionProxy connectionProxy;
   protected final StatementProxy statementProxy;
   final ResultSet delegate;

   protected ResultSetProxy(ConnectionProxy connectionProxy, StatementProxy statementProxy, ResultSet resultSet)
   {
      this.connectionProxy = connectionProxy;
      this.statementProxy = statementProxy;
      this.delegate = resultSet;
   }

   @SuppressWarnings("unused")
   final SQLException checkException(SQLException e)
   {
      return connectionProxy.checkException(e);
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public String toString()
   {
      return this.getClass().getSimpleName() + '@' + System.identityHashCode(this) + " wrapping " + delegate;
   }

   // **********************************************************************
   //                 Overridden java.sql.ResultSet Methods
   // **********************************************************************

   /**
    * {@inheritDoc}
    */
   @Override
   public final Statement getStatement() throws SQLException
   {
      return statementProxy;
   }

   @Override
   @SuppressWarnings("unchecked")
   public final <T> T unwrap(Class<T> iface) throws SQLException
   {
      if (iface.isInstance(delegate))
      {
         return (T) delegate;
      } else if (delegate != null)
      {
         return delegate.unwrap(iface);
      }

      throw new SQLException("Wrapped ResultSet is not an instance of " + iface);
   }
}
